//
//  QHConfiguredObj.m
//  IMFiveApp
//
//  Created by chen on 14-8-30.
//  Copyright (c) 2014年 chen. All rights reserved.
//

// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com

#import "QHConfiguredObj.h"
#import <boost/property_tree/ptree.hpp>
#import "boost/property_tree/xml_parser.hpp"
using namespace std;
using namespace boost::property_tree;

@interface QHConfiguredObj()
{
    iptree   xmlPtree;
}

@end

@implementation QHConfiguredObj

+ (QHConfiguredObj *)defaultConfigure
{
    static QHConfiguredObj *configureObj = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
    {
        configureObj = [[QHConfiguredObj alloc] init];
    });
    
    return configureObj;
}

- (id)init
{
    self = [super init];
    
    _themename = [[NSUserDefaults standardUserDefaults] objectForKey:kTHEME_TAG];
    _themefold = [[NSUserDefaults standardUserDefaults] objectForKey:kTHEMEFOLD_TAG];
    [self setResourse];
    return self;
}

- (void)setThemename:(NSString *)themename
{
    _themename = themename;
    [[NSUserDefaults standardUserDefaults] setObject:_themename forKey:kTHEME_TAG];
}

- (void)setThemefold:(NSString *)themefold
{
    _themefold = themefold;
    [self setResourse];
    [[NSUserDefaults standardUserDefaults] setObject:_themefold forKey:kTHEMEFOLD_TAG];
}


- (void)setResourse
{
    //主题路径
    NSString *documentsDirectory = [NSString stringWithFormat:@"%@%@",DE.mainPath,@"Resourse"];
    NSString * ResourseFile = nil;
    if (_themefold != nil &&  _themefold.length > 0)
    {
        ResourseFile = [[documentsDirectory stringByAppendingPathComponent:_themefold] stringByAppendingPathComponent:@"Resourse.xml"];
    }
    else
    {
        ResourseFile = [[NSBundle mainBundle] pathForResource:@"Resourse.xml" ofType:nil];
    }
    
    
    try
    {
        read_xml([ResourseFile UTF8String], xmlPtree);
    }
    catch(ptree_error &e)
    {
        NSLog(@"读取资源配置文件失败%@",[NSString stringWithUTF8String:e.what()]);
    }

}

- (NSString*) getValue:(NSString*)name
{
    string value = xmlPtree.get([name UTF8String],"");
    
    return [NSString stringWithUTF8String:value.c_str()];
}

@end
